package cn.wolfcode.shop.web.controller;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.domain.PropertyValue;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.service.IPropertyValueService;
import cn.wolfcode.shop.vo.JsonResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CatalogPropertyController {
    @Reference
    private IPropertyService propertyService;
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IPropertyValueService propertyValueService;

    /**
     * 商品分类属性
     * @param model
     * @return
     */
    @RequestMapping("property")
    public String property(Model model){

        List<Catalog> catalogs = catalogService.selectAll();
        model.addAttribute("allCatalog",JSON.toJSONString(catalogs));
        return "property/property";
    }

    /**
     * 商品属性列表
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("property/get/{id}")
    public String property(@PathVariable Long id, Model model){
        List<Property> propertyList=propertyService.selectByCatalogId(id);
        model.addAttribute("propertyList",propertyList);

        return "property/property_list";
    }

    /**
     * 商品属性值
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("propertyValue/get/{id}")
    public String propertyValue(@PathVariable Long id, Model model){
        model.addAttribute("propertyId",id);

        List<PropertyValue> propertyValuesList=propertyValueService.selectByPropertyId(id);
        model.addAttribute("propertyValueList",propertyValuesList);

        return "property/property_value_list";
    }

    /**
     * 保存或修改商品属性值
     * @param propertyValues
     * @return
     */
    @RequestMapping("propertyValue/save")
    @ResponseBody
    public JsonResultVO propertyValueSaveOrUpdate(@RequestBody List<PropertyValue> propertyValues){
        propertyValueService.saveOrUpdate(propertyValues);
        return JsonResultVO.success();
    }

    /**
     * 商品分类属性编辑
     * @param property
     * @return
     */
    @RequestMapping("property/save")
    @ResponseBody
    public JsonResultVO propertySave(Property property){
        propertyService.saveOrUpdateProperty(property);
        return JsonResultVO.success();
    }

    /**
     * 删除商品属性
     * @param id
     * @return
     */
    @RequestMapping("property/delete/{id}")
    @ResponseBody
    public JsonResultVO propertyDelete(@PathVariable Long id){
        propertyService.propertyDelete(id);
        return JsonResultVO.success();
    }






}
